/* File: poly.cpp Author: Katherine Gibson (gibsonk@seas) Desc: example of polymorphism using Animal and its child classes Cat, Dog, and Bird -- definitions */ using namespace std; #include #include "poly.h" /******************************** * ANIMAL CLASS -- PARENT CLASS * ********************************/ Animal::Animal(string name) { m_name = name; } //functions void Animal::Eat() { cout << m_name << " is " << "eating some food" << endl; } void Animal::Speak() { cout << m_name << " says " << "????" << endl; } void Animal::Perform() { Speak(); } /******************************************** * BIRD CLASS -- INHERITS FROM ANIMAL CLASS * ********************************************/ Bird::Bird(string name) : Animal(name) { // deliberately empty } //functions void Bird::Eat() { cout << m_name << " is " << "eating seeds" << endl; } void Bird::Speak() { cout << m_name << " says " << "chirp chirp" << endl; } void Bird::Perform() { Speak(); } /******************************************* * CAT CLASS -- INHERITS FROM ANIMAL CLASS * *******************************************/ Cat::Cat(string name) : Animal(name) { // deliberately empty } //functions void Cat::Eat() { cout << m_name << " is " << "eating meat" << endl; } void Cat::Speak() { cout << m_name << " says " << "meow" << endl; } void Cat::Perform() { Speak(); } /******************************************* * DOG CLASS -- INHERITS FROM ANIMAL CLASS * *******************************************/ Dog::Dog(string name) : Animal(name) { // deliberately empty } //functions void Dog::Eat() { cout << m_name << " is " << "eating bacon! So excited! Bacon!" << endl; } void Dog::Speak() { cout << m_name << " says " << "woof woof bark" << endl; } void Dog::Perform() { Speak(); }